(function($){
	$.util = {
		len:function(str){ //判断字符长度
			return str.replace(/[^\x00-\xff]/g,"**").length;
		},
		checkedval:function(ele){
			var ret = [];
			ele.each(function(){
				ret.push(this.value);
			});
			return ret;
		},
		show:function(){
			$(".state_tip").html("<img src=\"template/admin/images/loading2.gif\" border=\"0\" align=\"absmiddle\"/>正在执行").fadeIn();
		},
		hide:function(){
			$(".state_tip").html("操作完成").fadeOut('slow');
		}
	}
})(jQuery);




(function($){
	$.oper = {
		
		url:'', //操作url
		init:function(url) 
		{ //初始化 
			this.url = url;
			return this;
		},
		edit:function(obj,id)
		{ //ajax编辑
			var $this = $(obj);
			
			if($this.find("input").length == 0){
				var txt		 = $this.text(),
					size	 = parseInt(txt.length),
					$span	 = $("<input type = 'text' value='"+txt+"' size='"+ (size>70?70:size) +"'/>");
					setValue = function()
					{ //向服务器提交修改后的数据
							var spanval = $span.val();
							if(spanval && spanval != txt)
							{
								$.util.show();
								var postdata = {value:spanval,id:id};
								$.ajax({
									type:'POST',
									url:"orderType",
									data:{"sort":spanval,"typeid":id},
									dataType:'text',
									success: function(message)
									{    
										//$.util.hide();

										if(message == "success")
										{ //数据更新成功
											$this.text(spanval);
											
										}else
										{ //数据更新失败
											$this.text(txt);
											alert("发生错误,请刷新页面后重新执行该操作","警告");
										}
										
									}
								});

							} 
							else 
							{
								$this.text(txt);
							}
					}
				$this.html($span);
				$span.focus().select();
				$span.keyup(
					function(event)
					{ 
						if(event.keyCode == "13")
						{
							setValue();

						}
				}).blur(function(){
					setValue();
					//location.reload();
				});
			}
			else {
				return ;
			}
		},

		bgcolor:function(obj,type) {
			$(obj).css('backgroundColor',type=='on' ? '#0b819f':'');
		},

	}
})(jQuery);

(function($)
{
	$.pageSize={
		setSize:function(e){

			//var $this=$(e);
			var size=$('#pagesize').val();
		//	var size=$this.val();
			//将页数保存到cookie中
			var date = new Date();
			date.setTime(date.getTime() + (5* 60 * 1000));
			$.cookie('pagesize', size, { expires: date }); 
			$.Ajaxsearch();
		}
	}
})(jQuery);

(function($){
	var ajaxsearch=function(page)
	{
		//获取搜索条件
		var page = arguments[0]?arguments[0] : 1;
		var $ordersn=$("input[name='ordersn']").val();

		var $starttime=$("#start").val();

    	var $endtime=$("#end").val();    //$("select option:selected").val();
    	var $areatype=$("select[name='areatype'] option:selected").val();
    	var $paytype=$("select[name='paytype'] option:selected").val();
		var $orderstatus=$("select[name='orderstatus'] option:selected").val();
		$.ajax(
		{
			type:'POST',
			url:"searchOrder",
			data:{"ordersn":$ordersn,'starttime':$starttime,'paytype':$paytype,"areatype":$areatype,"endtime":$endtime,"orderstatus":$orderstatus,"page":page,},
			dateType:'JSON',
			success:function(result)
			{	
				var result=jQuery.parseJSON(result);
				var $page=result.pagearr; //底部分页
          		var $orders=result.orders;//订单
          		var $tbody=$('<tbody></tbody>');
          		var $string='';
          if (result.status)
				{
					
					$.each($orders,function(key,order){
						//console.log(value.companyname+key);
						$string+="<tr>";
            			$string+='<td><input type="checkbox" name="subBox" value='+order.orderid +"></td>";
						$string+="<td>"+order.orderid +"</td>";
						$string+="<td>"+order.order_sn+"</td>";
						$string+="<td>"+order.number +"</td>";
						$string+="<td>"+order.price +"</td>";
						$string+="<td>"+order.supperprice +"</td>";
            
						$string+="<td>"+order.pay_type +"</td>"; 
						$string+="<td>"+order.companyname +"</td>";
						$string+="<td>"+order.create_time +"</td>";
            			$string+="<td>"+order.allstatus +"</td>";
            			$string+="<td><span class='am-icon-pencil-square-o'></span><a href=editOrder/orderid/"+order.orderid+">详情</td>";
						$string+="<td> <span class='am-icon-trash-o'></span><a href='javascript:void(0)' class='delorder' value="+order.orderid+">删除</td>";
						$string+="</tr>";	
					});
					$tbody.append($string);
					$($tbody).replaceAll(".ordertable tbody");

					//替换分页
					$(".count").html($page.count);
					$(".currentpage").html($page.page);
					//$("#pagesize").val($page.pagesize);
					$('.prepage').attr('value',$page.prevpage);
					$('.nexpage').attr('value', $page.nextpage);
					$('.laspage').attr('value',$page.pagetotal);
				}else
				{
					 $tbody.append(result.message);
					$($tbody).replaceAll('.ordertable tbody');
				}
			}

		});
	}

 $.Ajaxsearch=ajaxsearch;

})(jQuery);

//翻页事件
(function($){
	 $.pageajax=function(name)
	{
		 var pagevalue=$('.'+name).attr('value');
		 $.Ajaxsearch(pagevalue);
		return false;
	}
})(jQuery);
(function(){
	$('#search').click(function(){
		  $.Ajaxsearch();
      event.preventDefault();
		

	});
})(jQuery);
//删除订单
(function($){
	$(document).on('click','.delorder',function(){
        var res=confirm("确定删除订单");
        if(!res){
          return false;
        }
        var id=$(this).attr('value');
        $.ajax({
            type:"GET",
            data:{"orderid":id},
            url: "delOrder",
            dataType:"json",
            success:function(message)
            {
                if (message.status)
                {
                    alert(message.msg);
                    location.reload();
                 }else{
                 alert(message.msg);
                  location.reload();
                }
            }
        })
   })
})(jQuery);
//全选
(function(){
	$(function(){
           $("#checkAll").click(function() {
                $('input[name="subBox"]').prop("checked",this.checked); 
            });
            var $subBox = $("input[name='subBox']");
            $subBox.click(function(){
                $("#checkAll").prop("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
            });
        }); 
})(jQuery);

//导出订单
(function(){
	 $(function(){
      $('.putcsv').click(function(){
          //获取所有选中订单
           var orderids="";
            $("input[name='subBox']:checkbox").each(function()
            { 
                if($(this).prop("checked"))
                {
                    orderids += $(this).val()+","

                }
            })
            
         window.open("putCsv?orderids="+orderids);
          
      });
  });
})(jQuery)
//日期插件
;(function($){
	//日期插件

	 $.start =
	   {
	    elem: '#start',
	    format: 'YYYY-MM-DD hh:mm:ss',
	    min: '1999-06-16 23:59:59', //设定最小日期为当前日期
	    max: '2099-06-16 23:59:59', //最大日期
	    istime: true,
	    istoday: false,
	    choose: function(datas)
		    {
		         end.min = datas; //开始日选好后，重置结束日的最小日期
		         end.start = datas //将结束日的初始值设定为开始日
		    }
	    };
	 $.end=
		 {
		    elem: '#end',
		    format: 'YYYY-MM-DD hh:mm:ss',
		    min: '1999-06-16 23:59:59',
		    max: '2099-06-16 23:59:59',
		    istime: true,
		    istoday: false,
		    choose: function(datas)
			    {
			        start.max = datas; //结束日选好后，重置开始日的最大日期
			    }
		 };
//laydate(start);
//laydate(end);
})(jQuery);

//冻结运营商
;(function($){
	$(function(){
		$('.frozen').click(function(){
			var result =confirm('确定冻结 此运营商');
			if (!result)
			{
				return '';
			};
			//冻结运营商
			var yid=$(this).attr('value')
			 $.ajax({
				        type:'POST',
				        url:"/admin.php/Home/Yunying/frozen",
				        data:{'yid':yid},
				        dataType:"json",
				        success:function(data)
				        {
				            if(data.status == false)
				            {
				             alert(data.msg);
				             return '';
				              
				            }
				            //删除此行元素
				            var pnode=$('.yid_'+yid);
				            $('tbody .yid_'+yid ).remove();
				            
				        }
				      }
			);
		});
	});
})(jQuery)
//检测运营商唯一性，根据登录帐号，和公司名称，不能重复
;(function($){
	$('.checksup').click(function(event){
  
    event.preventDefault();
    var $action=$("input[name='action']").val();
    var $cname=$("input[name='companyname']").val();
    var $name=$("input[name='name']").val();
    var $supperid=$("input[name='supperid']").val();
    switch($action)
    { 
      case 'addsup':
        $.ajax({
          type:'POST',
          url:'/index.php/Admin/Supper/weiyi',
          data:{"companyname":$cname,"name":$name,"action":$action},
          dataType:'json',
          success:function(message)
          { 
            if(message.status ==false) 
              {
                alert(message.msg);

              }else{
               $("#supper").submit();
              }
          }
        })
      break;
      case 'editsup':
      	$.ajax({
          type:'POST',
          url:'/index.php/Admin/Supper/weiyi',
          data:{"companyname":$cname,"name":$name,"action":$action,"supperid":$supperid},
          dataType:'json',
          success:function(message)
          { 
            if(message.status ==false) 
              {
                alert(message.msg);

              }else{
               $("#supper").submit();
              }
          }
        })
      	// $("#yun").submit();
      break;
    }
  
  })
})(jQuery)

//统一检测表单必填字段
;(function($){
	$.reqfild=function(e)
	{
			switch(e)
			{
			case "supper":
				$('#supper').validate({
			    rules:{
			            name:{
			              required:true,
			              minlength:2,
			            },
			            companyname:{
			            	required:true,
			              	minlength:3,
			            },
			            connectname:{
			            	required:true,
			              	minlength:2,
			            },
			            
			            mobile:{
			            	required:true,
			              	minlength:2,
			              },
			             address:{
			            	required:true,
			            	minlength:2,
			            },
			            pass: {
                        	required: false,
                        	minlength: 3
                    	},
                    	repass:{
                    		required: false,
                        	minlength: 3,
                        	equalTo: "#pass"
                    	}
			            
			    	}
			  });
			break;
			case '':
			break;

			}
	}
})(jQuery);
//品牌模块
(function($){
    $.Brand=
    {
        //search 品牌开始
        search:function()
        {
            var keywords=$("input[name='keywords']").val();
             var   keywords=(keywords== false) ?'':keywords;
             $.ajax({
                        type:'post',
                        url:"/index.php/Admin/Brand/searBrand",
                        data:{'keywords':keywords},
                        dataType:"json",
                        success:function(result)
                        {
                            var statu =result.status;
                            var brands=result.brands;

                            if(statu)
                            {
                                var $tbody=$('<tbody></tbody>');
                                var $string='';
                                $.each(brands,function(key,brand){
                                    $string+='<tr><td><input type="checkbox"></td>';
                                    $string+='<td>'+brand.brandid+'</td>';
                                    $string+='<td><a href="#">'+brand.brandname+'</a></td>';
                                    $string+='<td>'+brand.createtime +'</td>';
                                    $string+='<td><div class=""><div class="am-btn-group am-btn-group-xs"><button class="am-btn am-btn-default am-btn-xs am-text-secondary"><span class="am-icon-pencil-square-o"><a href="/admin.php/Home/Brand/editBrand/brandid/'+brand.brandid+'">编辑</a></span> </button>';
                                    $string+='<button class="am-btn am-btn-default am-btn-xs am-text-danger"><span class="am-icon-trash-o"> <a href="javascript:void(0)" onclick="ajaxdel('+brand.brandid+')"> 删除 </a> </span> </button></div></div></td>';
                                })
                               $tbody.append($string);
                               $($tbody).replaceAll(".brandtalbe tbody");
                               $('.brandpage').hide();

                            }else{
                                
                                $('.brandtalbe tbody').html('暂无数据');
                                 $('.brandpage').hide();
                            }
                        }
                      }
                      );    
        
        },
        //search 品牌结束

    };

    $('#searbrand').click(function(){$.Brand.search()});


})(jQuery)